package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.open.scanner.dao.entity.AttachDO;
import com.wlyuan.open.scanner.dao.mapper.AttachMapper;
import com.wlyuan.open.scanner.domain.attach.Attach;
import com.wlyuan.open.scanner.domain.order.AttachConvert;
import com.wlyuan.open.scanner.repository.AttachRepository;
import com.wlyuan.open.scanner.utils.enums.InvalidEnum;
import com.wlyuan.open.scanner.utils.enums.order.AttachTypeEnum;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AttachRepositoryImpl implements AttachRepository {
    private final AttachMapper attachMapper;

    @Override
    public List<Attach> getByOrderId(Long orderId) {
        return Optional.ofNullable(orderId).map(id -> {
            var list = select(id, null);
            return AttachConvert.convert(list);
        }).orElse(null);
    }

    @Override
    public List<Attach> getByContractId(Long contractId) {
        return Optional.ofNullable(contractId).map(id -> {
            var list = select(id, AttachTypeEnum.REJECT.getValue());
            return AttachConvert.convert(list);
        }).orElse(null);
    }

    @Override
    public Attach getById(Long attachId) {
        return Optional.ofNullable(attachId).map(id -> {
            var data = attachMapper.selectById(id);
            return Optional.ofNullable(data).map(attach -> {
                processUrl(Arrays.asList(attach));
                return AttachConvert.convert(attach);
            }).orElse(null);
        }).orElse(null);
    }

    private List<AttachDO> select(Long orderId, Integer type) {
        if (!LongUtils.notNullLong(orderId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<AttachDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttachDO::getAttachOwnerid, orderId)
                .isNotNull(AttachDO::getAttachName)
                .isNotNull(AttachDO::getAttachUrl)
                .eq(AttachDO::getAttachInvalid, InvalidEnum.ENABLE.getValue());
        if (type != null) {
            queryWrapper.eq(AttachDO::getAttachType, type);
        }

        var list = attachMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        list = list.stream().filter(x -> StringUtils.isNotBlank(x.getAttachUrl())).collect(Collectors.toList());
        processUrl(list);
        return list;
    }

    private void processUrl(List<AttachDO> list) {
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(data -> {
                if (StringUtils.isNotBlank(data.getAttachUrl())) {
                    data.setAttachUrl(data.getAttachUrl().replace("@1", ""));
                    if (StringUtils.isBlank(data.getAttachName())) {
                        data.setAttachName(data.getAttachUrl().substring(data.getAttachUrl().lastIndexOf("/") + 1, data.getAttachUrl().length()));
                    }
                }
            });
        }
    }
}
